{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "initial_id",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "import cv2\n",
    "\n",
    "from src.utils.pic.cv2plt import show_opencv_img_by_plt\n",
    "from src.classify.predict.load_model import load_model, predict_cv_image\n",
    "from src.classify.utils.devices_selector import get_recommended_device\n",
    "from src.config import config\n",
    "from src.classify.predict.predict_file import predict_validate_code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a2b31d083f40395d",
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "device = get_recommended_device()\n",
    "\n",
    "model_equal_symbol, model_operator, model_digit = load_model(device)\n",
    "\n",
    "test_dir_path = os.path.join(config.prj_root_path, \"src\", \"classify\", \"predict\", \"test\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "99e85ebb1a374bb1",
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def predict_img_file_test(file_path: str):\n",
    "    img = cv2.imread(file_path)\n",
    "\n",
    "    calc_result, calc_str, _, _, _, _ = predict_validate_code(\n",
    "        img,\n",
    "        device,\n",
    "        model_equal_symbol,\n",
    "        model_operator,\n",
    "        model_digit\n",
    "    )\n",
    "\n",
    "    show_opencv_img_by_plt(img)\n",
    "    print(calc_str)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d7945a56d0ef49c8",
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "predict_img_file_test(os.path.join(test_dir_path, \"test1_20240102160004_server.png\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "853facc4958d5ef2",
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "predict_img_file_test(os.path.join(test_dir_path, \"test2_20240102160811_server.png\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3c3ac44cfe87128a",
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "predict_img_file_test(os.path.join(test_dir_path, \"test3_20240102160857_server.png\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7f236e3abd9fb0ba",
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "predict_img_file_test(os.path.join(test_dir_path, \"test4_20240102160902_server.png\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7f0f6349fcb15d49",
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "predict_img_file_test(os.path.join(test_dir_path, \"test5_20240102160141_server.png\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2cba29647348b917",
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "predict_img_file_test(os.path.join(test_dir_path, \"test6_20240102160146_server.png\"))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
